/*
	File:		CreateLaserMessage.cpp
	Course:		SGD 1301
	Author:		Peter Nguyen
	Purpose:	Message to create a laser entity
*/

#include "CreateLaserMessage.h"

#include "Laser.h"
#include "../SGD Wrappers/SGD_Math.h"

////////////////////////////////////////////////////////////
// CONSTRUCTOR
CCreateLaserMessage::CCreateLaserMessage(CEntity* _pOwner, float fPosX, float fPosY, float fRotation )
	: CMessage( MSG_CREATE_LASER )		// call parent's constructor
{
	// Store the parameters
	m_fPosX		= fPosX;
	m_fPosY		= fPosY;
	m_fRotation	= fRotation;
	m_pOwner	= _pOwner;
	_pOwner->AddRef();
}


////////////////////////////////////////////////////////////
// DESTRUCTOR
CCreateLaserMessage::~CCreateLaserMessage(void)
{
	m_pOwner->Release();
}

////////////////////////////////////////////////////////////
// CreateLaser	- create a mostly-functional laser entity
//				  using the message's data members
//				- return the laser upcasted to CEntity*
CEntity* CCreateLaserMessage::CreateLaser( void ) const
{
	CLaser* pBullet = new CLaser();
	pBullet->SetOwner(m_pOwner);

	pBullet->SetWidth(32);
	pBullet->SetHeight(64);
	pBullet->SetRotation(m_pOwner->GetRotation());

	tVector2D forwardVector = {0,-1};
	forwardVector = Vector2DRotate(forwardVector,m_pOwner->GetRotation());

	pBullet->SetPosX(m_pOwner->GetPosX());//+forwardVector.fX*m_pOwner->GetWidth()*0.5f);//force to shoot at nose
	pBullet->SetPosY(m_pOwner->GetPosY());//+forwardVector.fY*m_pOwner->GetHeight()*0.5f);//force to shoot at nose

	forwardVector = forwardVector * 1000;

	if(m_pOwner->GetSpeed()>0)
	{
		forwardVector.fX += m_pOwner->GetVelX();
		forwardVector.fY += m_pOwner->GetVelY();
	}

	pBullet->SetVelX(forwardVector.fX);
	pBullet->SetVelY(forwardVector.fY);
	
	return (CEntity*)pBullet;
}
